package com.tb.ClassSchedule;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ToxicBakery.viewpager.transforms.DepthPageTransformer;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

/**
 * Created by guich on 2016/8/17.
 */
public class MainDayView extends ViewPager {

    private List<View>     viewList;
    private LinearLayout[] day;

    public MainDayView(Context context, AttributeSet attrs) {
        super(context, attrs);

        View[] classViews = new View[7];

        viewList = new ArrayList<>();
        viewList.add(new View(context));
        for (int i = 0; i < 7; i++) {
            classViews[i] = LayoutInflater.from(context).inflate(R.layout.one_day_layout, null);
            viewList.add(classViews[i]);
        }
        viewList.add(new View(context));

        day = new LinearLayout[]{
                (LinearLayout) classViews[0].findViewById(R.id.classes_layout),
                (LinearLayout) classViews[1].findViewById(R.id.classes_layout),
                (LinearLayout) classViews[2].findViewById(R.id.classes_layout),
                (LinearLayout) classViews[3].findViewById(R.id.classes_layout),
                (LinearLayout) classViews[4].findViewById(R.id.classes_layout),
                (LinearLayout) classViews[5].findViewById(R.id.classes_layout),
                (LinearLayout) classViews[6].findViewById(R.id.classes_layout),
        };

        ((TextView) classViews[0].findViewById(R.id.weekday)).setText("星期一");
        ((TextView) classViews[1].findViewById(R.id.weekday)).setText("星期二");
        ((TextView) classViews[2].findViewById(R.id.weekday)).setText("星期三");
        ((TextView) classViews[3].findViewById(R.id.weekday)).setText("星期四");
        ((TextView) classViews[4].findViewById(R.id.weekday)).setText("星期五");
        ((TextView) classViews[5].findViewById(R.id.weekday)).setText("星期六");
        ((TextView) classViews[6].findViewById(R.id.weekday)).setText("星期日");

        PagerAdapter adapter = new PagerAdapter() {
            @Override
            public int getCount() {
                return viewList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                //                super.destroyItem(container, position, object);
                container.removeView(viewList.get(position));

            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                //                return super.instantiateItem(container, position);
                container.addView(viewList.get(position));
                return viewList.get(position);

            }
        };

        setAdapter(adapter);
        setPageTransformer(true, new DepthPageTransformer());

//        setCurrentItem(TimeManager.getCurrentWeekday());
        addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float onPageScrolled, int positionOffsetPixels) {
                //                Log.d(getLocalClassName(),"onPageScrolled: " + onPageScrolled + " " + onPageScrolled);
            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    setCurrentItem(viewList.size() - 2);
                } else if ((position == viewList.size() - 1)) {
                    setCurrentItem(1);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                //                Log.d(getLocalClassName(),"onPageScrollStateChanged: " + state);

            }
        });
    }

    public void drawClasses(HashSet<Lesson> lessonsInfos, int selectedWeek) {
        setCurrentItem(TimeManager.getCurrentWeekday());

        View view;

        Lesson lesson  = new Lesson();
        Lesson lesson2 = new Lesson();              //记录上课时间未到的课程

        //绘制课程
        for (int i = 0; i < LessonSet.dayCount; i++) {
            day[i].removeAllViews();

            boolean isHoliday = true;
            for (int j = 0; j < LessonSet.sectionCount; j++) {


                boolean notTime = false;                            //记录是否存在上课时间未到的课程
                boolean interrupted = false;                        //人工中断标记
                for (Lesson lessonsInfo : lessonsInfos) {

                    lesson = lessonsInfo;

                    if ((lesson.getStartSection() == j + 1) && (lesson.getWeekday() == i + 1)) {
                        isHoliday = false;
                        if ((lesson.getStartWeek() <= selectedWeek) && (lesson.getEndWeek() >= selectedWeek)) {
                            notTime = false;
                            interrupted = true;
                            break;
                        } else {
                            lesson2 = lesson;
                            notTime = true;
                        }
                    }
                }

                if(!interrupted){
                    //说明没匹配到完全匹配的课程，再看是否存在时间未到的课程
                    if (notTime) {
                        lesson = lesson2;
                    } else {
                        lesson = new Lesson();
                    }
                }

                int n = 1 + lesson.getEndSection() - lesson.getStartSection();

                view = new LessonViewForDay(getContext(), lesson , notTime);
                if (notTime) {
                    view.setAlpha(0.3f);
                }
                day[i].addView(view);
                j = j + n - 1;
            }
            if(isHoliday){
//                day[i].setBackgroundColor(Color.BLACK);
                view = View.inflate(MainActivity.main,R.layout.holiday_view,null);
                day[i].addView(view);
            }

        }


    }


}
